package com.eastfair.builder.service.impl;

import com.eastfair.builder.entity.Exhibitor;
import com.eastfair.builder.service.BuilderExhibitionCompanyService;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.BizException;
import com.eastfair.venuebooking.api.ExhibitionCompanyFeign;
import com.eastfair.venueservice.dto.ExhibitionCompanyDTO;
import com.eastfair.venueservice.enumeration.ExhibitionCompanyCompanySourceEnum;
import com.eastfair.venueservice.enumeration.ExhibitionCompanyCompanyTypeEnum;
import com.eastfair.venueservice.vo.ServiceProviderVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 展会企业服务.
 *
 * @author dqq
 * @date 2022 /10/8
 */
@Service
@Slf4j
public class BuilderExhibitionCompanyServiceImpl implements BuilderExhibitionCompanyService {

    @Resource
    private ExhibitionCompanyFeign exhibitionCompanyFeign;

    @Override
    public boolean saveExhibitionCompany(ExhibitionCompanyDTO exhibitionCompanyDTO) {
        log.info("saveExhibitionCompany - 保存展会企业, exhibitionCompanyDTO={}", exhibitionCompanyDTO);
        R<Boolean> result = exhibitionCompanyFeign.saveExhibitionCompany(exhibitionCompanyDTO);
        log.info("saveExhibitionCompany - 完成, result= {}", result);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
        return result.getData();
    }

    @Override
    public boolean saveBatch(List<ExhibitionCompanyDTO> list) {
        log.info("saveBatch - 批量保存展会企业, list={}", list);
        R<Boolean> result = exhibitionCompanyFeign.saveBatch(list);
        log.info("saveBatch - 完成, result={}", result);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
        return result.getData();
    }

    @Override
    public boolean removeByServiceProviderId(List<Long> serviceProviderIds, Long exhibitionManageId) {
        log.info("removeByServiceProviderId - 服务商ID，展会ID删除展会企业, serviceProviderId={}, exhibitionManageId={}",
                serviceProviderIds, exhibitionManageId);
        ExhibitionCompanyDTO exhibitionCompanyDTO = new ExhibitionCompanyDTO();
        exhibitionCompanyDTO.setServiceProviderIdList(serviceProviderIds);
        exhibitionCompanyDTO.setExhibitionManageId(exhibitionManageId);
        R<Boolean> result = exhibitionCompanyFeign.removeByServiceProviderId(exhibitionCompanyDTO);
        log.info("removeByServiceProviderId - 完成, result={}", result);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
        return result.getData();
    }

    @Override
    public boolean saveByExhibitor(Exhibitor exhibitor) {
        log.info("saveByExhibitor - 通过展商保存展会企业, exhibitor={}", exhibitor);
        ExhibitionCompanyDTO exhibitorExhibitionCompanyDTO = toExhibitionCompanyDTO(exhibitor);
        return saveExhibitionCompany(exhibitorExhibitionCompanyDTO);
    }

    @Override
    public boolean saveByBuilder(ServiceProviderVO serviceProviderVO, Exhibitor exhibitor) {
        log.info("saveByBuilder - 通过搭建商保存展会企业, serviceProvider={}", serviceProviderVO);
        ExhibitionCompanyDTO exhibitionCompanyDTO = toExhibitionCompanyDTO(serviceProviderVO, exhibitor);
        return saveExhibitionCompany(exhibitionCompanyDTO);
    }

    /**
     * 展商转为展会企业
     *
     * @param exhibitor 展商
     * @return 展会企业
     */
    private ExhibitionCompanyDTO toExhibitionCompanyDTO(Exhibitor exhibitor) {
        ExhibitionCompanyDTO exhibitorExhibitionCompanyDTO = new ExhibitionCompanyDTO();
        exhibitorExhibitionCompanyDTO.setCompanyName(exhibitor.getExhibitorName());
        exhibitorExhibitionCompanyDTO.setCompanyType(ExhibitionCompanyCompanyTypeEnum.EXHIBITOR);
        exhibitorExhibitionCompanyDTO.setCompanySource(ExhibitionCompanyCompanySourceEnum.SYSTEM_OPERATE);
        exhibitorExhibitionCompanyDTO.setExhibitionManageId(exhibitor.getExhibitionManageId());
        exhibitorExhibitionCompanyDTO.setExhibitionManageName(exhibitor.getExhibitionManageName());
        exhibitorExhibitionCompanyDTO.setIsServiceProvider(BusinessConstant.NO);
        exhibitorExhibitionCompanyDTO.setIsExhibitor(BusinessConstant.YES);
        exhibitorExhibitionCompanyDTO.setExhibitorId(exhibitor.getId());
        exhibitorExhibitionCompanyDTO.setExhibitorName(exhibitor.getExhibitorName());
        exhibitorExhibitionCompanyDTO.setContact(exhibitor.getContact());
        exhibitorExhibitionCompanyDTO.setPhone(exhibitor.getContactMobile());
        exhibitorExhibitionCompanyDTO.setHasSite(BusinessConstant.YES);
        exhibitorExhibitionCompanyDTO.setSiteType(exhibitor.getSiteType().getCode());
        exhibitorExhibitionCompanyDTO.setSiteName(exhibitor.getSiteName());
        exhibitorExhibitionCompanyDTO.setSiteArea(exhibitor.getSiteArea());
        exhibitorExhibitionCompanyDTO.setSpaceCode(exhibitor.getSpaceCode());
        exhibitorExhibitionCompanyDTO.setSpaceName(exhibitor.getSpaceName());
        return exhibitorExhibitionCompanyDTO;
    }

    /**
     * 搭建商转为展会企业
     *
     * @param serviceProviderVO 搭建服务商
     * @param exhibitor         展商
     * @return 展会企业
     */
    private ExhibitionCompanyDTO toExhibitionCompanyDTO(ServiceProviderVO serviceProviderVO, Exhibitor exhibitor) {
        ExhibitionCompanyDTO exhibitionCompanyDTO = new ExhibitionCompanyDTO();
        exhibitionCompanyDTO.setCompanyCode(serviceProviderVO.getCompanyCode());
        exhibitionCompanyDTO.setCompanyName(serviceProviderVO.getCompanyName());
        ExhibitionCompanyCompanyTypeEnum exhibitionCompanyCompanyTypeEnum
                = ExhibitionCompanyCompanyTypeEnum.get(serviceProviderVO.getServiceProviderType());
        if (exhibitionCompanyCompanyTypeEnum != null) {
            exhibitionCompanyDTO.setCompanyType(exhibitionCompanyCompanyTypeEnum);
        }
        exhibitionCompanyDTO.setCompanySource(ExhibitionCompanyCompanySourceEnum.SYSTEM_OPERATE);
        exhibitionCompanyDTO.setExhibitionManageId(exhibitor.getExhibitionManageId());
        exhibitionCompanyDTO.setExhibitionManageName(exhibitor.getExhibitionManageName());
        exhibitionCompanyDTO.setIsServiceProvider(BusinessConstant.YES);
        exhibitionCompanyDTO.setServiceProviderId(serviceProviderVO.getId());
        exhibitionCompanyDTO.setServiceProviderName(serviceProviderVO.getCompanyName());
        exhibitionCompanyDTO.setIsExhibitor(BusinessConstant.NO);
        exhibitionCompanyDTO.setContact(serviceProviderVO.getPrincipalName());
        exhibitionCompanyDTO.setPhone(serviceProviderVO.getPrincipalMobile());
        exhibitionCompanyDTO.setHasSite(BusinessConstant.YES);
        exhibitionCompanyDTO.setSiteType(exhibitor.getSiteType().getCode());
        exhibitionCompanyDTO.setSiteName(exhibitor.getSiteName());
        exhibitionCompanyDTO.setSiteArea(exhibitor.getSiteArea());
        exhibitionCompanyDTO.setSpaceCode(exhibitor.getSpaceCode());
        exhibitionCompanyDTO.setSpaceName(exhibitor.getSpaceName());
        return exhibitionCompanyDTO;
    }
}